package com.feng.yupaoback.job;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feng.yupaoback.pojo.entity.User;
import com.feng.yupaoback.service.UserService;
import jakarta.annotation.Resource;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Data
@Component
@Slf4j
public class CheckTask {

    // 重点用户
    private List<Integer> mainUserIds = List.of(2501);

    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate<String, Object> myRedisTemplate;

    /**
     * 定时写入缓存
     */
    @Scheduled(cron = "0 0 */12 * * *")
    public void doCacheTask() {
        ValueOperations<String, Object> valueOperations = myRedisTemplate.opsForValue();
        for(Integer userId : mainUserIds){
            String redisKey = String.format("yupao-recommend-user-%S", userId);
            Page<User> userList = userService.getRecommendUsers(10, 1);
            try {
                valueOperations.set(redisKey, userList, 36000000, TimeUnit.MILLISECONDS);
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
    }
}
